import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-04-07
 * Time: 15:53
 */
public class TreeBianli {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode createNode() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.right = G;
        C.left = F;
        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
    // 后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    //计算节点个数
    public int Treenumber(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int ret = Treenumber(root.left) + Treenumber(root.right) + 1;
        return ret;
    }

    public static int SizeNode;

    public void size(TreeNode root) {
        if(root == null) {
            return;
        }
        SizeNode++;
        size(root.left);
        size(root.right);
    }


    public int leavesSize(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }

        return leavesSize(root.left) + leavesSize(root.right);

    }

    public int Leaves;

    public void leavesSize2(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            Leaves++;
        }
        leavesSize2(root.left);
        leavesSize2(root.right);

    }

    public int getdikcengleave(TreeNode root, int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getdikcengleave(root.left,k-1)
                + getdikcengleave(root,k-1);

    }

    //时间复杂度为O(N)
    public int HeightTree(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHigh = HeightTree(root.left);
        int rightHigh = HeightTree(root.right);

        return leftHigh > rightHigh ? leftHigh + 1 : rightHigh + 1;

        //return HeightTree(root.left)>HeightTree(root.right)?HeightTree(root.left)+ 1:HeightTree(root.right)+ 1;
    }


    public TreeNode find(TreeNode root,char val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode ret1 = find(root.left,val);
        if(ret1 != null) {
            return ret1;
        }
        TreeNode ret2 = find(root.right,val);
        if(ret2 != null) {
            return ret2;
        }
        return null;
       /* find(root.left,val);
        find(root.right,val);
        return null;*/
    }



    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null) {
            return true;
        }
        if(p == null && q != null || p != null && q == null) {
            return false;
        }
        if(q.val != p.val) {
            return false;
        }

        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }


    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) {
            return false;
        }

        if(isSameTree(root,subRoot)) {
            return true;
        }

        if(isSubtree(root.left,subRoot)) {
            return true;
        }

        if(isSubtree(root.right,subRoot)) {
            return true;
        }

        return false;
    }



    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        if(root.left == null && root.right == null) {
            return root;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);

        return root;
    }

    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }

        int rootleft = HeightTree(root.left);
        int rootright = HeightTree(root.right);

        return Math.abs(rootleft - rootright) <= 1 &&
                isBalanced(root.left) && isBalanced(root.right);

    }

    public boolean isBalanced2(TreeNode root) {
        if(root == null) {
            return true;
        }
        return MaxTreeNode(root) >= 1;
    }
    public int MaxTreeNode(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int rootleft = MaxTreeNode(root.left);
        if(rootleft < 0) {
            return -1;
        }
        int rootright = MaxTreeNode(root.right);
        if(rootright < 0) {
            return -1;
        }
        if(Math.abs(rootleft-rootright) <= 1) {
            return Math.max(rootleft,rootright) + 1;
        }else {
            return -1;
        }
    }


    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }
        return isSymmetricleftright(root.left,root.right);
    }

    public boolean isSymmetricleftright(TreeNode rootleft,TreeNode rootright) {
        if(rootleft == null && rootright == null) {
            return true;
        }
        if(rootleft != null && rootright == null || rootleft == null && rootright != null) {
            return false;
        }
        if(rootleft.val != rootright.val) {
            return false;
        }

        return isSymmetricleftright(rootleft.left,rootright.right) &&
                isSymmetricleftright(rootleft.right,rootright.left);
    }

    /*class TreeNode {
        TreeNode left;
        TreeNode right;
        public char val;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    public class Main {
        public static int i = 0;
        public static TreeNode createTree(String s) {
            TreeNode root = null;
            if(s.charAt(i) != '#') {
                root = new TreeNode(s.charAt(i));
                i++;
                root.left = createTree(s);
                root.right = createTree(s);
            }else {
                i++;
            }
            return root;
        }

        public static void inOrder(TreeNode root) {
            if(root == null) {
                return;
            }
            inOrder(root.left);
            System.out.print(root.val + " ");
            inOrder(root.right);
        }

        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                String s = in.nextLine();
                TreeNode root = createTree(s);
                inOrder(root);
            }
        }*/


    public void levelOrder(TreeNode root) {
        if(root == null) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }

        }
        System.out.println();


    }

    /*public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list1 = new ArrayList<>();
        if(root == null) {
            return list1;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            while(size > 0) {
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if(cur.left != null) {
                    queue.offer(cur.left);
                }
                if(cur.right != null) {
                    queue.offer(cur.right);
                }
                size--;
            }
            list1.add(list);
        }

        return list1;*/


    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }

        if(p == root || q == root) {
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);

        if(left != null && right != null) {
            return root;
        }else if(left != null && right == null) {
            return left;
        }else {
            return right;
        }
    }

    /*public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }

        Stack<TreeNode> sp = new Stack<>();
        Stack<TreeNode> sq = new Stack<>();

        getPath(root,p,sp);
        getPath(root,q,sq);
        int sizesq = sq.size();
        int sizesp = sp.size();
        int size = sizesq - sizesp;
        if(size > 0) {
            while(size > 0) {
                sq.pop();
                size--;
            }
        }else {
            size = sizesp - sizesq;
            while(size > 0) {
                sp.pop();
                size--;
            }
        }
        while(!sp.empty() && !sq.empty()) {
            if(sp.peek() == sq.peek()) {
                return sp.peek();
            }else {
                sp.pop();
                sq.pop();
            }
        }
        return null;
    }

    public boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack) {
        if(root == null) {
            return false;
        }

        stack.push(root);

        if(root == node) {
            return true;
        }

        boolean flg1 = getPath(root.left,node,stack);
        if(flg1) {
            return true;
        }

        boolean flg2 = getPath(root.right,node,stack);
        if(flg2) {
            return true;
        }
        stack.pop();
        return false;
    }*/


    /*public void PreorderTraversal(TreeNode root) {
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }

            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    public void InorderTraversal(TreeNode root) {
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    public void PostorderTraversal(TreeNode root) {
        TreeNode cur = root;
        TreeNode prev = null;
        Stack<TreeNode> stack = new Stack<>();
        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if(top.right == null || prev == top.right) {
                stack.pop();
                System.out.print(top.val + " ");
                prev = top;
            }else {
                cur = top.right;
            }
        }
    }



    public List<Character> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        List<Character> List = new ArrayList<>();
        TreeNode cur = root;
        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                List.add(cur.val);
                cur = cur.left;
            }

            TreeNode top = stack.pop();
            cur = top.right;
        }
        return List;
    }
*/



/*    class Solution {
        public int Index;
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            return buildTreeChild(preorder,inorder,0,inorder.length-1);
        }

        public TreeNode buildTreeChild(int[] preorder, int[] inorder,int inBegin,int inEnd) {
            if(inBegin > inEnd) {
                return null;
            }

            TreeNode root = new TreeNode(preorder[Index]);
            int rootIndex = findIndex(preorder,inorder,inBegin,inEnd);
            Index++;

            root.left = buildTreeChild(preorder,inorder,inBegin,rootIndex-1);
            root.right = buildTreeChild(preorder,inorder,rootIndex+1,inEnd);
            return root;
        }

        public int findIndex(int[] preorder, int[] inorder,int inBegin,int inEnd) {
            for(int i = 0; i <= inEnd; i++) {
                if(preorder[Index] == inorder[i]) {
                    return i;
                }
            }
            return -1;
        }*/

//    class Solution {
//        public int Index = 0;
//        public TreeNode buildTree(int[] inorder, int[] postorder) {
//            Index = postorder.length - 1;
//            return buildTreeChild(inorder,postorder,0,inorder.length-1);
//        }
//
//        public TreeNode buildTreeChild(int[] inorder,int[] postorder,int inBegin,int inEnd) {
//            if(inBegin > inEnd) {
//                return null;
//            }
//            TreeNode root = new TreeNode(postorder[Index]);
//            int rootIndex = findIndex(inorder,postorder,inBegin,inEnd);
//            Index--;
//
//            root.right = buildTreeChild(inorder,postorder,rootIndex+1,inEnd);
//            root.left = buildTreeChild(inorder,postorder,inBegin,rootIndex-1);
//            return root;
//        }
//
//
//        private int findIndex(int[] inorder,int[] postorder,int inBegin,int inEnd) {
//            for(int i = 0; i <= inEnd; i++) {
//                if(postorder[Index] == inorder[i]) {
//                    return i;
//                }
//            }
//            return -1;
//        }
//
//    }


    class Solution {
        public String tree2str(TreeNode root) {
            StringBuilder stringbuilder = new StringBuilder();
            stringBuilder(root,stringbuilder);
            return stringbuilder.toString();
        }

        public void stringBuilder(TreeNode root,StringBuilder stringbuilder) {
            if(root == null) {
                return;
            }
            stringbuilder.append(root.val);
            if(root.left != null) {
                stringbuilder.append("(");
                stringBuilder(root.left,stringbuilder);
                stringbuilder.append(")");
            }else {
                if(root.right == null) {
                    return;
                }else {
                    stringbuilder.append("()");
                }
            }

            if(root.right != null) {
                stringbuilder.append("(");
                stringBuilder(root.right,stringbuilder);
                stringbuilder.append(")");
            }else {
                return;
            }

        }
    }









}
